#ifndef TRASH_H
#define TRASH_H

#include <list>

#include "OSHeaders.h"

template <typename T>
class CTrash
{
public:
	CTrash() : m_Limit(10) {}

	virtual ~CTrash() { FreeAll();}
public:
	T *	Allocate()
	{
		T *pTmp = NULL;
		if(!m_List.empty())
		{
			pTmp = m_List.front();
			m_List.pop_front();
		}
		else
		{
			pTmp = new T;
			if(!pTmp)
			{
				//log
				printf("Allocate failed\n");
			}
		}
		return pTmp;
	}

	void	Reclaim(T *p)
	{
		if(m_List.size() < m_Limit)
			m_List.push_back(p);
		else
			delete p;
	}
	
	//limit the trash size
	void	SetLimit(UInt32 iCount) { m_Limit = iCount;}
	
	//status
	Bool	IsEmpty() {return m_List.empty();}
	
	//free
	void	FreeAll()
	{
		while(!m_List.empty())
		{
			delete m_List.front();
			m_List.pop_front();
		}
	}
	
protected:
	typedef typename std::list<T*>	TrashList;
	typedef typename std::list<T*>::iterator	TrashListIterator;	
	
protected:
	TrashList	m_List;
	UInt32		m_Limit;
};
#endif

